package com.liumuxian.user.service.impl;

import com.liumuxian.user.dao.ProblemDao;
import com.liumuxian.user.pojo.Problem;
import com.liumuxian.user.service.ProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import util.IdWorker;

import javax.persistence.criteria.*;
import java.util.*;

@Slf4j
@Service
@CrossOrigin
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    ProblemDao problemDao;

    @Autowired
    private IdWorker idWorker;
    @Override
    public Problem save(Problem problem) {
        problem.setId(idWorker.nextId()+"");
        problem.setState(1);
        problem.setCreatedate(new Date());
        return problemDao.save(problem);
    }

    @Override
    public void delete(String id) {
        problemDao.deleteById(id);
    }

    @Override
    public Problem saveAndFlush(Problem problem) {
        return problemDao.saveAndFlush(problem);
    }

    @Override
    public Problem getOne(String id) {
        return problemDao.getOne(id);
    }

    @Override
    public List<Problem> findAll() {
        Map map = new HashMap();
        map.put("state",0);
        Specification<Problem> specification = createSpecification(map);
        return problemDao.findAll(specification);
    }
    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Problem> createSpecification(Map searchMap) {

        return new Specification<Problem>() {

            @Override
            public Predicate toPredicate(Root<Problem> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                    //获取cache中的key的class，用于判断key的类型
                    Class<? extends Object> keyClass = null;
                    keyClass = searchMap.get("id").getClass();
                    //判断是不是HashSet类型
                    if(keyClass.equals(HashSet.class)){
                        Path<Object> path = root.get("id");
                        CriteriaBuilder.In<Object> in = cb.in(path);
                        Set<String> id = (Set<String>) searchMap.get("id");
                        id.forEach(i -> in.value(i));
                        predicateList.add(in);
                    }else {
                        predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                    }
                }
                // 考试名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                //考试属性
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 状态
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.gt(root.get("state"),(Number)searchMap.get("state")));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }


}
